define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var from_id = $('#fromId').val(),from = $('#from').val();
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'finance/index',
                    del_url: 'finance/del',
                    multi_url: 'finance/multi',
                    table: 'user_money_log',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url + '?from_id=' + from_id + '&from=' + from,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                // commonSearch: true,
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('Uid'), operate: 'LIKE'},
                        {field: 'username', title: __('Name'), align: 'left', operate: 'LIKE', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'before', title: __('Before'), operate: false},
                        {field: 'money', title: __('Price'), operate: false},
                        {field: 'after', title: __('After'), operate: false},
                        {field: 'name', title: __('From')},
                        {field: 'from_id', title: __('FromId')},
                        {field: 'memo', title: __('Mark'), operate: false},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        withdraw: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'finance/withdraw',
                    auth_url: 'finance/approved',
                    multi_url: 'finance/multi',
                    table: 'user_money_log',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                // commonSearch: true,
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'uid', title: __('Uid'), operate: 'LIKE'},
                        {field: 'username', title: __('Name'), align: 'left', operate: 'LIKE', formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }
                        },
                        {field: 'amount', title: __('WthdrawNum'), operate: false},
                        {field: 'coin_name', title: __('CoinName'), operate: false},
                        {field: 'to_address', title: __('ToAddress'), operate: false},
                        {field: 'tradeid', title: __('TradeId')},
                        {field: 'status_str', title: __('Status'), operate: false},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {name: 'approved', text: __('Approved'),   icon: '', confirm: '确定审核通过吗？', classname: 'btn btn-xs btn-success btn-ajax', url: 'finance/approved'},
                                {name: 'reject', text: __('Reject'),   icon: '', confirm: '确定驳回吗？', classname: 'btn btn-xs btn-danger btn-magic btn-ajax', url: 'finance/reject'},
                            ],events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        approved: function () {
            Controller.api.bindevent();//这个地方如果不加，会造成你有跳转页面  不会出现延迟的那种消息通知
        },
        reject: function () {
            Controller.api.bindevent();//这个地方如果不加，会造成你有跳转页面  不会出现延迟的那种消息通知
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
